package com.mhy.crash;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.mhy.crash.R;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


/**
 * Created by wanjian on 2018/1/22.
 * recyclerview的bindviewholder 方法抛出异常
 */

public class SecondAct extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.second);

        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);

        setAdapter(recyclerView);

    }

    private void setAdapter(final RecyclerView recyclerView) {
        //setTag(int key, Object value)方法
        //这个方法的key传入一个唯一的int类型值都不行，得在资源文件中定义一个id类型得字段
//        int tag = android.R.id.text1; //这个值是android系统定义的 setTag key异常。。。。
//        int itemLayout = android.R.layout.simple_list_item_1;
        int tag = R.id.text1;
        int itemLayout = R.layout.item;

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration decoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        decoration.setDrawable(getResources().getDrawable(android.R.drawable.divider_horizontal_bright));
        recyclerView.addItemDecoration(decoration);
        recyclerView.setAdapter(new RecyclerView.Adapter() {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(getApplicationContext()).inflate(itemLayout, parent, false);
                TextView textView = view.findViewById(tag);
                view.setTag(tag, textView);
//                if (parent == recyclerView) {
//                    throw new RuntimeException("RecyclerView onCreateViewHolder出错");
//                }
                return new RecyclerView.ViewHolder(view) {
                };
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
                // recyclerview的bindviewholder 方法抛出异常
                ((TextView) holder.itemView.getTag(tag)).setText(String.valueOf(position));

                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(SecondAct.this, "POSITION " + position, Toast.LENGTH_SHORT).show();
                        throw new RuntimeException("RecyclerView 点击出错 POSITION: " + position);
                    }
                });
                if (position == 20) {
                    Log.e("onBindViewHolder", "第" + position+"条数据出错");
                    throw new RuntimeException("RecyclerView onBindViewHolder出错 POSITION: " + position);
                }
            }

            @Override
            public int getItemCount() {
                return 100;
            }
        });
    }
}
